﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using FiveStone.MyStack;


namespace FiveStone
{
    public partial class Form1 : Form
    {
        //public bool first=true;
        public Form1()
        {
            InitializeComponent();
        }

        public Boards bd;        
        private void Form1_Load(object sender, EventArgs e)
        {            
            
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < 680 && e.Y < 680)
            {
                int m = (int)(e.X / 42);
                int n = (int)(e.Y / 42);
                if (m < 0){ m = 0; }
                if (n < 0){ n = 0; }
                if (m > 14){ m = 14; }
                if (n > 14){ n = 14; }
                label1.Text = "X：" + m.ToString() + "  Y：" + n.ToString();
                toolStripStatusLabel1.Text = "落子点：X " + m.ToString() + " Y " + n.ToString() + "";
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(bd!=null)
                bd.PersonPut(e.X, e.Y);
            
            //if (historylist.Items.Count == 0)
            //{
            //    historylist.Items.Add(bd.game.st.GetPerson());
            //    historylist.Items.Add(bd.game.st.GetPC());
            //}
            //else
            //{
            //    if (historylist.Items[historylist.Items.Count-1].ToString() != bd.game.st.GetPC() && historylist.Items[historylist.Items.Count - 2].ToString() != bd.game.st.GetPerson())
            //    {
            //        historylist.Items.Add(bd.game.st.GetPerson());
            //        historylist.Items.Add(bd.game.st.GetPC());
            //    }
            //}
        }                

        private void 玩家先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!玩家先ToolStripMenuItem.Checked)
            {
                historylist.Items.Clear();
                //bd.Start(true);//玩家先                
                玩家先ToolStripMenuItem.Checked = true;
                电脑先ToolStripMenuItem.Checked = false;
                black.Text = User.MyName;
                white.Text = User.PlayerName;
                label2.Text = "1";
            }
        }

        private void 电脑先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!电脑先ToolStripMenuItem.Checked)
            {
                historylist.Items.Clear();
                //bd.Start(false); //电脑先
                //historylist.Items.Add(bd.game.st.GetPC());                
                电脑先ToolStripMenuItem.Checked = true;
                玩家先ToolStripMenuItem.Checked = false;
                black.Text = User.PlayerName;
                white.Text = User.MyName;
                label2.Text = "0";
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 单机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartGame(true);
        }
        

        /// <summary>
        /// 创建主机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 联机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Online onlineFrm = new Online();
            onlineFrm.ShowDialog();
            if (onlineFrm.DialogResult == DialogResult.OK)
            {
                SocketHelper.mfrom = this;                
                StartGame(false);
            }
            if (onlineFrm.DialogResult == DialogResult.Yes)
            {
                SocketHelper.mfrom = this;
                FiveStone.Handle.SendJoin();
                StartGame(false);
            }
        }       

        public void setInfo(bool isSelf){
            historylist.Items.Clear();
            bd.Start(isSelf);//玩家先                
            black.Text = "玩家";
            white.Text = "对方";
            label2.Text = "1";
        }

        private void StartGame(bool isPc)
        {            
            historylist.Items.Clear();
            System.Drawing.Graphics g = this.CreateGraphics();
            bd = new Boards(g, new Game(),historylist);            
            bd.ClearBoards();
            //bd.DrawBoard(picFive);
            picFive.Visible = false;
            bd.DrawBoard();
            bd.PlayerIsPC = isPc;//对方是否为电脑
            if (isPc)//对手是否电脑
            {
                if (label2.Text == "0")
                {
                    bd.Start(false);//电脑先  对方先                    
                }
                else
                {
                    bd.Start(true);//玩家先  我先
                }
            }
            //else
            //{
            //    bd.Start(User.isMeFirst);
            //}            
        }

        public void Start()
        {
            bd.Start(User.isMeFirst);
            MessageBox.Show("开始游戏!");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SocketHelper.Close();
            Close();
        }
        
        
    }
}
